***MASTERBUILD 2006-2009***
clear
set obs 1
gen blank =1
save ${nc_raw_data}/temp, replace

forvalues t=2006/2009 {
	use ${nc_raw_data}/mb_`t'_pub, clear
	gen schoolyr = `t'
	append using ${nc_raw_data}/temp
	save ${nc_raw_data}/temp, replace
	tab schoolyr, m
}

drop if blank==1
drop blank

foreach s0 in rd ma a1 e1 {
	local s = substr("`s0'",1,1)
	destring `s0'_score , force gen(`s'_score)
	egen `s'_mean_gt = mean(`s'_score), by(`s0'_test_id schoolyr)
	egen `s'_sd_gt = sd(`s'_score), by(`s0'_test_id schoolyr)
	gen `s'_sds = (`s'_score-`s'_mean_gt)/`s'_sd_gt

	rename `s0'_exemption_code `s'_exem
	gen `s'_tdt = date(`s0'_test_date,"YMD")
}
replace ethnic = ethnicity if schoolyr==2009
keep schoolyr grade lea schlcode reporting_year ethnic sex daysmem ec eds frl tas lep_current lep_year lep_exit_year lep_rbih section504 homeless migrant neglected delinquent pared mem_fds mem_mar mem_fdf mem_20d mastid r_* m_* a_* e_*
save ${nc_raw_data}/mb2006_2009_clean, replace

***MASTERBUILD 2010-2012***
clear
set obs 1
gen blank =1
save ${nc_raw_data}/temp, replace

forvalues t=2010/2012 {
	use ${nc_raw_data}/mb_`t'_pub, clear
	gen schoolyr = `t'
	append using ${nc_raw_data}/temp
	save ${nc_raw_data}/temp, replace
	tab schoolyr, m
}

drop if blank==1
drop blank

foreach s0 in rd ma a1 e1 {
	local s = substr("`s0'",1,1)
	rename `s0'_scoreN `s'_score
	egen `s'_mean_gt = mean(`s'_score), by(`s0'_test_id schoolyr)
	egen `s'_sd_gt = sd(`s'_score), by(`s0'_test_id schoolyr)
	gen `s'_sds = (`s'_score-`s'_mean_gt)/`s'_sd_gt

	rename `s0'_exemption_code `s'_exem
	rename `s0'_test_dt `s'_tdt
}
rename ethnicity ethnic
keep schoolyr grade lea schlcode reporting_year collection_code grade minority_code ethnic sex in_fdf in_mar in_fds daysmem leamem b504 aig_r aig_m swd ec_code eds frl lep lep_status titlei_model tas_r tas_m ayp_tas_r ayp_tas_m migrant mastid r_* m_* a_* e_*
save ${nc_raw_data}/mb2010_2012_clean, replace

*/



***MASTERBUILD 2013-2016***
clear
set obs 1
gen blank =1
save ${nc_raw_data}/temp2, replace

forvalues t=2013/2016 {
	use ${nc_raw_data}/pcaudit_pub`t', clear
	gen schoolyr = `t'
	append using ${nc_raw_data}/temp2
	save ${nc_raw_data}/temp2, replace
	tab schoolyr, m
}

drop if blank==1
drop blank

foreach s0 in rd ma a1 e2 {
	local s = substr("`s0'",1,1)
	rename pc_`s0'_score `s'_score
	egen `s'_mean_gt = mean(`s'_score), by(pc_`s0'_type grade schoolyr)
	egen `s'_sd_gt = sd(`s'_score), by(pc_`s0'_type grade schoolyr)
	gen `s'_sds = (`s'_score-`s'_mean_gt)/`s'_sd_gt

	rename pc_`s0'_accom `s'_exem
	rename pc_`s0'_date `s'_tdt
}
tostring daysmem, replace
keep mastid schoolyr lea schlcode birthdt daysmem daysmem_f daysmem_s daysabs grade ethnic sex lep lep_year_exited lep_first_year lep_exempt lep_exempt_part swd ec_code eds frl aig s504 migrant in_fds leamem r_* m_* a_* e_*
save ${nc_raw_data}/mb2013_2016_clean, replace


***MASTERBUILD 1997-2005***
clear
set obs 1
gen blank =1
save ${nc_raw_data}/temp3, replace

forvalues t=1997/2005 {
	use ${nc_raw_data}/mb_`t'_pub, clear
	gen schoolyr = `t'
	append using ${nc_raw_data}/temp3
	save ${nc_raw_data}/temp3, replace
	tab schoolyr, m
}

drop if blank==1
drop blank

rename mthPstSc mthPostSc
foreach s0 in alg1 eng1 rd mth  {
	local s = substr("`s0'",1,1)
	capture rename `s0'PostSc `s'_score
	capture rename `s0'scor `s'_score
	egen `s'_mean_gt = mean(`s'_score), by(grade year)
	egen `s'_sd_gt = sd(`s'_score), by(grade year)
	gen `s'_sds = (`s'_score-`s'_mean_gt)/`s'_sd_gt
	capture rename `s'pstexem `s'_exem
	capture rename `s0'exem `s'_exem
}
keep schoolyr bdate mastid lea schlcode grade ethnic sex daysNIV f_diplma schlname m_* r_* a_* e_*
save ${nc_raw_data}/mb1997_2005_clean, replace


use ${nc_raw_data}/mb1997_2005_clean, clear
append${nc_raw_data}/mb2006_2009_clean
append ${nc_raw_data}/mb2010_2012_clean
append${nc_raw_data}/mb2013_2016_clean
save${nc_clean_data}/mb_full, replace

erase ${nc_raw_data}/mb1997_2005_clean
erase ${nc_raw_data}/mb2006_2009_clean
erase ${nc_raw_data}/mb2010_2012_clean
erase ${nc_raw_data}/mb2013_2016_clean
